//
//  QSFLoginView.m
//  QSF_Dev
//
//  Created by Mr.Dynasty on 2018/6/12.
//  Copyright © 2018年 Mr.Dynasty. All rights reserved.
//

#import "LoginView.h"

@interface LoginView()

/**
 账号
 */
@property (strong, nonatomic) WLCTextField *accountTextField;

/**
 密码
 */
@property (strong, nonatomic) WLCTextField *passwordTextField;

/**
 忘记密码
 */
@property (strong, nonatomic) YYLabel *forgetLabel;

/**
 登录
 */
@property (strong, nonatomic) UIButton *loginBtn;

/**
 登录回调
 */
@property (copy, nonatomic) LoginBlock login;

/**
 忘记密码回调
 */
@property (copy, nonatomic) dispatch_block_t forget;

@end

@implementation LoginView

- (instancetype)init
{
    self = [super init];
    if (self) {
        [self makeLoginView];
    }
    return self;
}

- (void)makeLoginView {
    
    UIImageView *loginImageView = [[UIImageView alloc] init];
    [self addSubview:loginImageView];
    loginImageView.image = [UIImage imageNamed:@"pic_01"];
    [loginImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(self.mas_centerX);
        make.top.equalTo(self).offset(107 * ySize);
        make.width.equalTo(102 * xSize);
        make.height.equalTo(100 * xSize);
    }];
    
    _accountTextField = [[WLCTextField alloc] init];
    [self addSubview:_accountTextField];
    _accountTextField.layer.cornerRadius = 3;
    _accountTextField.backgroundColor = DefaultBackColor;
    
    _accountTextField.placeHolder = NSLocalizedString(@"请输入账号", nil);
    _accountTextField.textField.font = RegularDefaultFont;    
    [_accountTextField mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.equalTo(self).offset(UIEdgeInsetsMake(0, 25 * xSize, 0, 25 * xSize));
        make.top.equalTo(loginImageView.mas_bottom).offset(71 * ySize);
        make.height.equalTo(50 * ySize);
    }];
    _accountTextField.leftBtn.frame = CGRectMake(0, 0, 50 * ySize, 50 * ySize);
    _accountTextField.rightBtn.frame = CGRectMake(0, 0, 50 * ySize, 50 * ySize);
    [_accountTextField.leftBtn setImage:[UIImage imageNamed:@"login_account"] forState:UIControlStateNormal];
    [_accountTextField.rightBtn setImage:[UIImage imageNamed:@"login_input_delete"] forState:UIControlStateNormal];
    [_accountTextField.rightBtn addTarget:self action:@selector(actionForDeleteAccount) forControlEvents:UIControlEventTouchUpInside];
    
    
    _passwordTextField = [[WLCTextField alloc] init];
    [self addSubview:_passwordTextField];
    _passwordTextField.layer.cornerRadius = 3;
    _passwordTextField.backgroundColor = DefaultBackColor;
    _passwordTextField.placeHolder = NSLocalizedString(@"请输入密码", nil);
    _passwordTextField.textField.font = RegularDefaultFont;
    _passwordTextField.textField.secureTextEntry = YES;
    [_passwordTextField mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.equalTo(self).offset(UIEdgeInsetsMake(0, 25 * xSize, 0, 25 * xSize));
        make.top.equalTo(self.accountTextField.mas_bottom).offset(15 * ySize);
        make.height.equalTo(50 * ySize);
    }];
    _passwordTextField.leftBtn.frame = CGRectMake(0, 0, 50 * ySize, 50 * ySize);
    _passwordTextField.rightBtn.frame = CGRectMake(0, 0, 50 * ySize, 50 * ySize);
    [_passwordTextField.leftBtn setImage:[UIImage imageNamed:@"login_password"] forState:UIControlStateNormal];
    [_passwordTextField.rightBtn setImage:[UIImage imageNamed:@"login_input_delete"] forState:UIControlStateNormal];
    [_passwordTextField.rightBtn addTarget:self action:@selector(actionForDeletePassword) forControlEvents:UIControlEventTouchUpInside];
    
    _forgetLabel = [[YYLabel alloc] init];
    [self addSubview:_forgetLabel];
    [_forgetLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(self).offset(-20 * xSize);
        make.top.equalTo(self.passwordTextField.mas_bottom).offset(15 * ySize);
    }];
    NSString *forgetString = NSLocalizedString(@"忘记密码？", nil);
    NSMutableAttributedString *attributedText = [[NSMutableAttributedString alloc] initWithString:forgetString];
    __weak typeof(self)weakself = self;
    [attributedText yy_setTextHighlightRange:NSMakeRange(0, forgetString.length) color:DefaultColor backgroundColor:DefaultColor tapAction:^(UIView * _Nonnull containerView, NSAttributedString * _Nonnull text, NSRange range, CGRect rect) {
        weakself.forget();
    }];
    [attributedText yy_setFont:RegularMiddelFont range:NSMakeRange(0, forgetString.length)];
    [attributedText yy_setUnderlineStyle:NSUnderlineStyleSingle range:NSMakeRange(0, forgetString.length)];
    _forgetLabel.attributedText = attributedText;
    _forgetLabel.hidden = YES;
    
    _loginBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [self addSubview:_loginBtn];
    [_loginBtn addTarget:self action:@selector(actionForLogin:) forControlEvents:UIControlEventTouchUpInside];
    _loginBtn.layer.cornerRadius = 3;
    [_loginBtn setBackgroundColor:HEX_COLOR(0xF7CE47)];
    [_loginBtn setTitle:NSLocalizedString(@"登录", nil) forState:UIControlStateNormal];
    _loginBtn.titleLabel.font = RegularDefaultFont;
    [_loginBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.equalTo(self).offset(UIEdgeInsetsMake(0, 25 * xSize, 0, 25 * xSize));
        make.top.equalTo(self.forgetLabel.mas_bottom).offset(35 * ySize);
        make.height.equalTo(50 * ySize);
    }];
    
}


- (void)clearPassword {
    self.passwordTextField.textField.text = @"";
}

- (void)actionForLogin:(UIButton *)buttton {
    _login(_accountTextField.textField.text,_passwordTextField.textField.text);
}

- (void)loginBtnClick:(LoginBlock)loginAction {
    _login = [loginAction copy];
}

- (void)forgetPasswordTap:(void (^)(void))forgetAction {
    _forget = [forgetAction copy];
}

- (void)actionForDeleteAccount {
    _accountTextField.textField.text = nil;
}

- (void)actionForDeletePassword {
    _passwordTextField.textField.text = nil;
}

@end
